package com.bluishoul.order;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;

import my.db.QueryHelper;
import my.mvc.RequestContext;

import com.bluishoul.action.OrderAction;
import com.bluishoul.bean.Pojo;

public class OrderDetail extends Pojo {

	public static OrderDetail INSTANCE = new OrderDetail();
	public static final byte TYPE_CREATE_DATA = 1;
	public static final byte TYPE_DETAIL_DATA = 0;
	private long id;
	private long order;
	private long dish;
	private long user;
	private String ident;// 用户标识符，用于非登陆用户
	private long shop;
	private Timestamp create_time;
	private byte type;
	private int count = 1;

	private String ident() {
		Cookie c = RequestContext.get().cookie(OrderAction.SESSION_ORDER_ID);
		if (c != null)
			return c.getValue();
		return null;
	}

	public List<Long> getOrderListByIdent(String ident, long user) {
		String sql = "";
		if (user == 0) {
			sql = "SELECT distinct `order` FROM menu_orderdetail WHERE ident=? ORDER BY create_time DESC";
			return QueryHelper.query(Long.class, sql,
					StringUtils.isBlank(ident) ? ident() : ident);
		} else {
			sql = "SELECT distinct `order` FROM menu_orderdetail WHERE user=? ORDER BY create_time DESC";
			return QueryHelper.query(Long.class, sql, user);
		}
	}
	public List<Long> listDishesByIdent(long order, String ident, long user,
			int type) {
		String sql = "";
		if (user == 0) {
			sql = "SELECT dish FROM menu_orderdetail WHERE `order` = ? and ident=? and type=?";
			return QueryHelper.query(Long.class, sql, order, ident, type);
		} else {
			sql = "SELECT dish FROM menu_orderdetail WHERE `order` = ? and user=? and type=?";
			return QueryHelper.query(Long.class, sql, order, user, type);
		}
	}

	public int deleteDishesByIdent(long order, String ident, long user, int type) {
		String sql = "";
		if (user == 0) {
			sql = "DELETE FROM menu_orderdetail WHERE `order` = ? and ident=? and type=? and count=1";
			return QueryHelper.update(sql, order, ident, type);
		} else {
			sql = "DELETE FROM menu_orderdetail WHERE `order` = ? and user=? and type=? and count=1";
			return QueryHelper.update(sql, order, user, type);
		}
	}

	public Map<Long, List<OrderDetail>> ListOrderDetailsOfShop(long order,
			int type) {
		List<OrderDetail> details = ListOrderDetails(order, type, 0);
		Map<Long, List<OrderDetail>> map = new HashMap<Long, List<OrderDetail>>();
		for (OrderDetail orderDetail : details) {
			if (map.containsKey(orderDetail.getShop())) {
				map.get(orderDetail.getShop()).add(orderDetail);
			} else {
				List<OrderDetail> ds = new ArrayList<OrderDetail>();
				ds.add(orderDetail);
				map.put(orderDetail.getShop(), ds);
			}
		}
		return map;
	}

	public List<OrderDetail> ListOrderDetails(long order, int type, long shop) {
		String sql = "SELECT * FROM menu_orderdetail WHERE `order` = ? and `type` = ?"
				+ (shop == 0 ? "" : " and shop = " + shop);
		return QueryHelper.query(OrderDetail.class, sql, order, type);
	}

	public long getUser() {
		return user;
	}

	public void setUser(long user) {
		this.user = user;
	}

	public String getIdent() {
		return ident;
	}

	public void setIdent(String ident) {
		this.ident = ident;
	}

	public long getOrder() {
		return order;
	}

	public void setOrder(long order) {
		this.order = order;
	}

	public long getDish() {
		return dish;
	}

	public void setDish(long dish) {
		this.dish = dish;
	}

	public long getShop() {
		return shop;
	}

	public void setShop(long shop) {
		this.shop = shop;
	}

	public Timestamp getCreate_time() {
		return create_time;
	}

	public void setCreate_time(Timestamp create_time) {
		this.create_time = create_time;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public static long[] Save(List<OrderDetail> details) {
		if (details == null)
			return null;
		long[] count = new long[details.size()];
		for (OrderDetail detail : details) {
			count[details.indexOf(detail)] = detail.Save();
		}
		return count;
	}

	public static int Delete(long[] ids) {
		int count = 0;
		for (long id : ids) {
			OrderDetail detail = INSTANCE.Get(id);
			if (detail.Delete())
				count++;
		}
		return count;
	}

	@Override
	protected String TableName() {
		return "menu_orderdetail";
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}
